<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Captcha Class
 *
 */
class Captcha {
	
	var $CI;
	var $code_str 	 = array(
							'1'=>'23456789',
							'2'=>'abcdefghgkmnpqrstwxyzABCDEFGHGKMNPQRSTWXYZ',
							'4'=>'路阳光子无开月了的比小山古天中石头井火大西木开人也叶九七六五合文只个鱼羊哈么点四生件格不步布各你田红蓝来很春下画京左百白东关工共足方尺才梦见查着去区曲寻早可上丰爱呵刘谷');
	var $code 			 = '';
	var $code_arr 		 = array();
	var $code_type 		 = 1;
	var $code_length 	 = 5;
	var $captcha 		 = '';
	var $width 			 = 75;
	var $height 		 = 23;
	var $dot_num 		 = 0;
	var $line_num 		 = 0;
	var $image;
	var $font_path 		 = 'fonts/simhei.ttf';
	var $font_name 		 = 'simhei.ttf';
	var $session_name 	 = 'captcha';
	var $color_backgroud = array('r' => 0, 'g' => 0, 'b' => 0);
	var $color_font 	 = array('r' => 255, 'g' => 255, 'b' => 255);
	var $color_line 	 = array('r' => 200, 'g' => 200, 'b' => 200);
	var $color_dot 		 = array('r' => 200, 'g' => 200, 'b' => 200);
	
	public function __construct($params = array())
    {
    	$this->CI =& get_instance();

		$this->CI->load->library('session');
		$attribute = array (
			
				'width',
				'height',
				'font_name',
				'code_type',
				'code_length',
				'session_name',
				'color_backgroud',
				'color_font',
				'color_line',
				'color_dot' 
		);
		foreach ( $attribute as $key)
		{
			if (isset($params[$key]))
			{
				$this->$key = $params[$key];
			}
		}
		
		// 设置字体路径
		$this->font_path = BASEPATH . $this->font_path;
    }
    
    /**
     * 根据要显示的验证码字符串类型组合验证码字符串
     */
    public function set_code_str()
    {
    	$this->code_type = intval($this->code_type);
    	$this->code_type = 1;
    	$this->code_type = $this->code_type > 0 && $this->code_type < 8 ? $this->code_type : 1 ;
    	
    	// 根据类型1，2，4做与运算，组合相应字符串
    	foreach ($this->code_str as $key => $value)
    	{
    		if (($key & $this->code_type) === $key)
    		{
    			$this->code .= $value;
    		}
    	}
    }
    
    /**
     * 随机生成验证码字符
     */
    public function create_code()
    {
    	$this->code_length = intval($this->code_length);
    	
    	// 验证码长度小于0，设置成5位
    	$this->code_length = $this->code_length >= 0 ? $this->code_length : 5;
    	preg_match_all("/./u", $this->code, $ar);
    	$code_len = count($ar['0']);
    	$code_array = $ar['0'];
    	for($i = 0 ; $i < $this->code_length ; $i++){
    		$this->code_arr[$i]  = $code_array[mt_rand(1,$code_len)-1];
    		$this->captcha = $this->captcha . $this->code_arr[$i];
    	}
    	$this->captcha = strtolower($this->captcha);
    }
    
    /**
     * 创建干扰线
     */
    function create_line()
    {
    	// 干扰线颜色
    	$c_l = imagecolorallocate($this->image, $this->color_line['r'], $this->color_line['g'], $this->color_line['b']);
    	 
        // 干扰线坐标
    	$position_x = mt_rand(10, 35);
    	$position_y = mt_rand(0, 6);
    	
    	//添加干扰线
    	for ($j = 0; $j < $this->line_num; $j++) {
    		$l_x1 = mt_rand(0, $position_x-10);
    		$l_y1 = mt_rand(0, $position_y+10);
    		$l_x2 = mt_rand($position_x+40,$position_x+60);
    		$l_y2 = mt_rand($position_y+5, 23);
    		imageline($this->image, $l_x1, $l_y1,$l_x2,$l_y2,$c_l);
    	}
    }
    
    /**
     * 创建躁点
     */
    function create_dot()
    {
        // 躁点颜色
    	$c_d = imagecolorallocate($this->image, $this->color_dot['r'], $this->color_dot['g'], $this->color_dot['b']);

    	//添加躁点
    	for ($j = 0; $j < $this->dot_num; $j++) {
    		imagesetpixel($this->image, mt_rand(0, $this->width - 1), mt_rand(0, $this->height - 1), $c_d);
    	}
    }
    
    /**
     * 创建文本
     */
    function create_text()
    {
        // 文字颜色
    	$c_f = imagecolorallocate($this->image, $this->color_font['r'], $this->color_font['g'], $this->color_font['b']);

    	// 每个字符偏移量
    	$str_l = 15;
    	for ($j2 = 0; $j2 < count($this->code_arr); $j2++) {
    		$x = mt_rand(2, 4) + $str_l*$j2;
    		$y = mt_rand(14, 22);
    		$str = iconv('utf-8', 'utf-8', $this->code_arr[$j2]);
    		imagettftext($this->image, mt_rand(11, 15), mt_rand(-50, 50), $x, $y, $c_f, $this->font_path,$str);
    	}
    }

    /**
     * 显示验证码图片
     */
    function show()
    {
    	// 根据要显示的验证码字符串类型组合验证码字符串
    	$this->set_code_str();

    	// 生成随机的验证码字符
    	$this->create_code();

    	// 将验证码存入数据库
    	$this->CI->session->set_userdata($this->session_name, $this->captcha);
    	//echo $this->captcha;exit;

    	// 创建图像，并设置大小
    	$this->image= imagecreatetruecolor($this->width, $this->height);

    	// 图像背景颜色
    	imagecolorallocate($this->image, $this->color_backgroud['r'], $this->color_backgroud['g'], $this->color_backgroud['b']);
    	
    	$this->create_line();
    	
    	$this->create_dot();
    	
    	$this->create_text();
    	//imagestring($this->image,6, $position_x, $position_y, $code, $c_f);
    	header("Content-type:image/jpeg");
    	imagejpeg($this->image);
    }
}

// END CI_Captcha class

/* End of file Captcha.php */
/* Location: ./system/libraries/Captcha.php */